package com.ebelter.bodyfatscale.ui.pager.register;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.ui.activity.AddUserInfoActivity;
import com.ebelter.bodyfatscale.ui.activity.GuestSelectWriteInfoActivity;
import com.ebelter.bodyfatscale.ui.activity.RegisterActivity;
import com.ebelter.bodyfatscale.util.AppUtils;
import com.ebelter.bodyfatscale.util.NumUtils;
import com.ebelter.bodyfatscale.util.ViewUtils;


/**
 * 身高页
 * Created by pan on 2018/2/27.
 */

public class HeightPager extends BasePager {

    private EditText etHeight;

    private int height;


    private TextView height_cm_tv1;
    private TextView height_cm_tv2;


    public HeightPager(Context context,int height) {
        super(context);
        this.height=height;
        //setHeight(height);
    }

    @Override
    protected int loadLayoutById() {
        return R.layout.pager_height;
    }

    @Override
    public void initViews() {
        etHeight = mRootView.findViewById(R.id.et_height_pager);
       // setHeight(height);

        etHeight.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable s) {

                String valueStr = etHeight.getText().toString().trim();


                if (TextUtils.isEmpty(valueStr)){
                    setActityNextBtText(false);
                }else {
                    setActityNextBtText(true);
                }

            }
        });


        height_cm_tv1 = mRootView.findViewById(R.id.height_cm_tv1);
        height_cm_tv2 = mRootView.findViewById(R.id.height_cm_tv2);

        displayUnit();

    }

    private void displayUnit() {
        String local= AppUtils.getLocalStr();
        if (TextUtils.equals(local,AppUtils.iw)){
            ViewUtils.goneView(height_cm_tv2);
            ViewUtils.displayView(height_cm_tv1);
        }else {
            ViewUtils.goneView(height_cm_tv1);
            ViewUtils.displayView(height_cm_tv2);
        }


    }


    //设置父类的下一步按钮是否可用
    private void setActityNextBtText(boolean isEnable){
        Activity activity = getActivity();
        if (activity instanceof RegisterActivity){
            ((RegisterActivity)activity).setbtn_next_register(isEnable);
        }else if (activity instanceof GuestSelectWriteInfoActivity){
            ((GuestSelectWriteInfoActivity)activity).setbtn_next_register(isEnable);
        }else if (activity instanceof AddUserInfoActivity){
            ((AddUserInfoActivity)activity).setbtn_next_register(isEnable);
        }
    }


    public void shake_etHeight(){
        ViewUtils.shakeView(etHeight);
    }



    public int getHeight(){
        return height=NumUtils.string2Int(etHeight.getText().toString().trim());
    }



    public void setHeight(int height){
        if (etHeight!=null){
            etHeight.setText(height+"");
            etHeight.setSelection(etHeight.getText().length());//将光标移至最后
        }
    }


}
